<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class MaterielPhone extends Model
{
  use Common;

  protected $table = 'materiel_phone';
  protected $fillable = [
      'member_id',
      'phone',
      'materiel_id'
  ];

  public function Materiel()
  {
    return $this->belongsTo(Materiels::class, 'materiel_id');
  }

  /**
   * 获取layui表格数据
   * @return \Illuminate\Http\JsonResponse
   */
  public static function LayUiTableData($filter = null, $member_id = null)
  {
    $list = self::select('id', 'materiel_id', 'phone', 'created_at')->with(['Materiel' => function ($materiel) {
      $materiel->select('id', 'name');
    }]);
    empty($member_id) ?: $list = $list->where('member_id', $member_id)->where('materiel_id', session('materiel_id'));
    if (!empty($filter)) {
      empty($filter['phone']) ?: $list = $list->where('phone', 'like', '%' . $filter['phone'] . '%');
    }
    $list = $list->latest('id')->paginate(self::getLimit($filter['limit']))->toArray();
    foreach ($list['data'] as &$vo) {
      $vo['materiel_name'] = $vo['materiel']['name'];
    }
    return self::returnLayUiData($list);
  }
}
